package test.script;

import java.util.Calendar;

import com.cait.framework.data.DBParameterCollection;
import com.cait.framework.data.DataTable;
import com.cait.framework.data.IDataBase;
import com.cait.framework.data.filter.DataBaseFilterHandle;
import com.cait.framework.data.filter.FilterDefinition;
import com.cait.framework.data.filter.enOperaters;
import com.cait.framework.data.oracleClient.OraDataBase;
import com.cait.framework.utils.Convert;
 
import com.cait.workflowserver.enactment.scriptengine.JavaScriptExecutor;
import com.cait.workflowserver.enactment.scriptengine.ScriptExecuteContext;
import com.cait.workflowserver.execution.FlowExecuteContext;
import com.cait.workflowserver.execution.execinstance.RelevantDataCollection;

import test.WorkFlowTest;

public class testScriptRunner   extends WorkFlowTest{

	public testScriptRunner(String arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
	}
	
	
	public void testRunner()
	{
		   FlowExecuteContext fl=new FlowExecuteContext();
           fl.setCurrentParticipantId("myName");
           JavaScriptExecutor jsrunner=new JavaScriptExecutor();
	 	RelevantDataCollection rds=new RelevantDataCollection();
	 	rds.setValueByName("mydata","712");
	  
	 	ScriptExecuteContext scontext=new ScriptExecuteContext();
		scontext.getVariables().add("out1",System.out);
		scontext.getVariables().add("myDataBase",new OraDataBase());
		//(new OraDataBase()).createParams()
		scontext.getVariables().add("rds",rds);
		scontext.getVariables().add("now",Calendar.getInstance());
		scontext.getVariables().add("current",fl);
	 	//scontext.getVariables().add("out",System.out);
	 	StringBuffer sb=new StringBuffer();
	 	//sb.append("importPackage(Packages.com.cait.framework);");
	 	sb.append("var main=function()\r\n");
	 	sb.append("{\r\n");
	 	sb.append("  var m5='555';\r\n");
	 	sb.append("  this.m6=function(){out1.println(m5);};\r\n");
	 	sb.append("   this.al={m:234,m2:323};\r\n");
	 	sb.append("   out1.println('测试');\r\n");
	 	sb.append("   out1.println('测试');\r\n");
	 	sb.append("   out1.println(rds.getValueByName('mydata'));\r\n");
	 	sb.append(" if(rds.getValueByName('mydata')>652)out1.println('大于');\r\n");
	 	sb.append("  ");
	// 	sb.append("   out1.println(com.cait.framework.utils.Convert.toString(now,'yyyy-MM-dd'));\r\n");
	 	
//		sb.append(" return true;\r\n");
//	 	sb.append(" ;\r\n");
//	 	sb.append(" ;\r\n");
//	 	sb.append(" ;\r\n");
//	 	sb.append(" ;\r\n");
//		sb.append(" ;\r\n");
//	 	sb.append(" ;\r\n");
//	 	sb.append(" ;\r\n");
//	 	sb.append(" ;\r\n");
//	 	sb.append(" ;\r\n");
//	 	sb.append(" ;\r\n");
	 	sb.append("}\r\n");
	 	sb.append("main.prototype.foo=function(){out1.println('输入原形');};\r\n");
	 	sb.append("var myMain=new main();\r\n");
		sb.append("out1.println(myMain.al.m);\r\n");
		sb.append("myMain.m6();\r\n");
		sb.append("var sql='select * from wf_proc_category  " +
	 		" where PROC_CATE_ID=:PROC_CATE_ID ';\r\n");	
		DBParameterCollection d;
	  DataTable dt;
	  
		sb.append("var parms=workflowdev.createParams();\r\n");
		sb.append("parms.add(':PROC_CATE_ID','c08d3fee-7151-459d-94c2-865625924201');\r\n");
		sb.append("var dataset=workflowdev.executeQuery(sql,parms);\r\n");
		sb.append(" out1.println(dataset.getRows().get(0).get('PROC_CATE_NAME').toString());");
		//sb.append("setTimeout(function(){myMain.foo()},1000);");
	 	//sb.append("myMain.foo();\r\n");
	 	//System.out.println
		//jsrunner.getScript().setScriptText(sb.toString()); 
	 
	 	//jsrunner.ExecuteScript("myMain.foo",scontext);
	 	jsrunner.ExecuteScript(null,scontext);//测试包
	 	
	 //	JsExpressionRunner js3=new JsExpressionRunner();
	 	//js3.getScript().setScriptText("[#current.getCurrentParticipantId()#]");
	 	//js3.getScript().setScriptText("[#'ee-sere-serse'#]");
	 //	js3.ExecuteScript(null,scontext);
	 	System.out.println("执行结果:"+scontext.getResult());
	 	OraDataBase b=new OraDataBase();
	 	DataBaseFilterHandle d3=new DataBaseFilterHandle(b);
//	 	 String sql= "Select WORK_ITEM_ID,ACT_INST_ID,WORK_ITEM_NAME"+
//	       ",WORK_ITEM_CREATE_DATE,WORK_ITEM_COMPLETE_DATE"+
//	       ",WORK_ITEM_SARIAL,WORK_ITEM_STATE"+
//	       ",WORK_ITEM_OWNER_ID,WORK_ITEM_TYPE,WORK_ITEM_DESC,WORK_ITEM_OWNER_NAME," +
//	       "PROC_INST_ID,PROC_INST_NAME"+
//	       " From WF_WORK_ITEM ";
	 	d3.setSqlCommandText("Select * From WF_WORK_ITEM");
	 	FilterDefinition filter=new FilterDefinition();
	 	//enOperaters df;
	 
	 filter.getConditionCollection().add("WORK_ITEM_CREATE_DATE",enOperaters.BETWEEN,Convert.toCalendar("2007-1-10"),Convert.toCalendar("2007-1-30"));
	 	//filter.getConditionCollection().add("WORK_ITEM_CREATE_DATE",enOperaters.BETWEEN,"2007-1-10","2007-1-30");
	
	 System.out.println("执行结果:" +d3.ExecuteFilter(filter).size());
	 
	  
		
	}

}
